﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;

namespace University.Workshop.WinApp.RatesAndGroupsFolder
{
    public partial class RatesGroupsForm : Form
    {
        DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
        DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();


        public RatesGroupsForm()
        {      
            InitializeComponent();
            
            gridRates.DataSource = rateTable;
            gridGroups.DataSource = groupTable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnRateRegister_Click(object sender, EventArgs e)
        {
            try
            {
                Rate rate = RateManager.RegisterNewRate(
                    txtCode.Text,
                    txtService.Text,
                    txtPrice.Text,
                    txtDescription.Text
                );

                DataAccess.RateDataAccess.InsertRate(rate);
                DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
                gridRates.DataSource = rateTable;
                cleanTextFields();
            }
            catch (Exception ex)
            {
                labelRateMessage.Text = ex.Message;
            }
        }

        private void btnRateDelete_Click(object sender, EventArgs e)
        {
            cleanTextFields();

            String value = String.Empty;
            DataGridViewRow row = gridRates.CurrentRow;
            if (row == null)
            {
                MessageBox.Show(this, "No se seleccionó ningún registro ", "Seleccionar Campo");
            }
            else
            {
                value = (string)row.Cells[0].Value;
                MessageBox.Show(this, "La tarifa fue eliminada exitosamente", "Eliminar Tarifa");
                bool controler = DataAccess.RateDataAccess.DeleteRate(value);
                DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
                gridRates.DataSource = rateTable;
            }
        }

        private void btnRateUpdate_Click(object sender, EventArgs e)
        {
            cleanTextFields();

            btnRateRegister.Enabled = false;
            btnRateDelete.Enabled = false;
            txtCode.Enabled = false;
            btnSave.Visible = true;
            btnRateUpdate.Enabled = false;

            DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
            gridRates.DataSource = rateTable;

            txtCode.DataBindings.Add("Text", rateTable, "Code");
            txtService.DataBindings.Add("Text", rateTable, "Service");
            txtPrice.DataBindings.Add("Text", rateTable, "Price");
            txtDescription.DataBindings.Add("Text", rateTable, "Description");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

            for (int i = 0; i < gridRates.RowCount; i++)
			{
                Rate rate = RateManager.UpdateRate(
                    gridRates.Rows[i].Cells[0].Value.ToString(),
                    gridRates.Rows[i].Cells[1].Value.ToString(),
                    gridRates.Rows[i].Cells[2].Value.ToString(),
                    gridRates.Rows[i].Cells[3].Value.ToString()
                );
                DataAccess.RateDataAccess.UpdateRate(rate);
            }
            cleanTextFields();
            btnSave.Visible = false;
            btnRateRegister.Enabled = true;
            btnRateDelete.Enabled = true;
            btnRateUpdate.Enabled = true;
            txtCode.Enabled = true;

            txtCode.DataBindings.Clear();
            txtService.DataBindings.Clear();
            txtPrice.DataBindings.Clear();
            txtDescription.DataBindings.Clear();

            DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
            gridRates.DataSource = rateTable;

            MessageBox.Show(this, "Los datos fueron actualizados exitosamente", "Modificiación de Tarifa");

            }
            catch (Exception ex)
            {
                labelRateMessage.Text = ex.Message;
            }
        }

        private void cleanTextFields()
        {
            txtCode.Clear();
            txtService.Clear();
            txtPrice.Clear();
            txtDescription.Clear();

            txtCompany.Clear();
            txtDiscount.Clear();

            labelRateMessage.Text = String.Empty;
            labelGroupMessage.Text = String.Empty;
        }

        private void btnGroupRegister_Click(object sender, EventArgs e)
        {
            try
            {
                Group group = GroupManager.RegisterNewGroup(
                    txtCompany.Text,
                    txtDiscount.Text
                );

                DataAccess.GroupDataAccess.InsertGroup(group);

                DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();
                gridGroups.DataSource = groupTable;


                cleanTextFields();
            }
            catch (Exception ex)
            {
                labelGroupMessage.Text = ex.Message;
            }
        }

        private void btnGroupUpdate_Click(object sender, EventArgs e)
        {
            cleanTextFields();

            btnGroupRegister.Enabled = false;
            btnGroupDelete.Enabled = false;
            txtCompany.Enabled = false;

            btnGroupSave.Visible = true;
            btnGroupUpdate.Visible = false;
            btnGroupRegister.Visible = false;
            btnGroupDelete.Visible = false;

            btnAddPatientGroup.Visible = false;

            DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();
            gridGroups.DataSource = groupTable;

            txtCompany.DataBindings.Add("Text", groupTable, "Company");
            txtDiscount.DataBindings.Add("Text", groupTable, "Discount");
        }

        private void btnGroupSave_Click(object sender, EventArgs e)
        {
            try
            {

            for (int i = 0; i < gridGroups.RowCount; i++)
            {
                Group group = GroupManager.UpdateGroup(
                    gridGroups.Rows[i].Cells[0].Value.ToString(),
                    gridGroups.Rows[i].Cells[1].Value.ToString()
                );
                DataAccess.GroupDataAccess.UpdateGroup(group);
            }

            cleanTextFields();

            btnGroupSave.Visible = false;
            btnGroupUpdate.Visible = true;
            btnGroupRegister.Visible = true;
            btnGroupDelete.Visible = true;

            btnAddPatientGroup.Visible = true;

            btnGroupRegister.Enabled = true;
            btnGroupDelete.Enabled = true;
            txtCompany.Enabled = true;

            txtCompany.DataBindings.Clear();
            txtDiscount.DataBindings.Clear();

            DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();
            gridGroups.DataSource = groupTable;

            MessageBox.Show(this, "Los datos fueron modificados exitosamente", "Modificiación de Grupos");

            }
            catch (Exception ex)
            {
                labelRateMessage.Text = ex.Message;
            }
        }

        private void btnGroupDelete_Click(object sender, EventArgs e)
        {
            cleanTextFields();

            String value = String.Empty;
            DataGridViewRow row = gridGroups.CurrentRow;
            if (row == null)
            {
                MessageBox.Show(this, "No se seleccionó ningún registro ", "Seleccionar Campo");
            }
            else
            {
                value = (string)row.Cells[0].Value;
                MessageBox.Show(this, "El grupo fue eliminado exitosamente", "Eliminar Grupo");

                bool controler = DataAccess.GroupDataAccess.DeleteGroup(value);

                DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();
                gridGroups.DataSource = groupTable;
            }
        }

        private void btnAddPatientGroup_Click(object sender, EventArgs e)
        {
            labelGroupMessage.Text = String.Empty;
            String value = String.Empty;
            DataGridViewRow row = gridGroups.CurrentRow;

            if (row == null)
            {
                MessageBox.Show(this, "No se seleccionó ningún registro ", "Seleccionar Campo");
            }
            else
            {
                value = (string)row.Cells[0].Value;
                
                SearchPatientGroup searchPatient = new SearchPatientGroup(value);
                searchPatient.ShowDialog();
            }
        }

        private void tabGroups_Click(object sender, EventArgs e)
        {
        }

        private void tabControl1_MouseClick(object sender, MouseEventArgs e)
        {
            changeTab();
        }

        private void changeTab()
        {
            cleanTextFields();

            btnGroupSave.Visible = false;
            btnGroupUpdate.Visible = true;
            btnGroupRegister.Visible = true;
            btnGroupDelete.Visible = true;

            btnAddPatientGroup.Visible = true;

            btnGroupRegister.Enabled = true;
            btnGroupDelete.Enabled = true;
            txtCompany.Enabled = true;

            txtCompany.DataBindings.Clear();
            txtDiscount.DataBindings.Clear();

            DataTable groupTable = DataAccess.GroupDataAccess.ShowGroups();
            gridGroups.DataSource = groupTable;


            btnSave.Visible = false;
            btnRateRegister.Enabled = true;
            btnRateDelete.Enabled = true;
            btnRateUpdate.Enabled = true;
            txtCode.Enabled = true;

            txtCode.DataBindings.Clear();
            txtService.DataBindings.Clear();
            txtPrice.DataBindings.Clear();
            txtDescription.DataBindings.Clear();

            DataTable rateTable = DataAccess.RateDataAccess.ShowRates();
            gridRates.DataSource = rateTable;
            
        }
    }
}
